package com.chkj.fbook.business.service.impl;

import com.chkj.fbook.business.service.SeatTimeService;
import com.chkj.fbook.common.utils.IDUtils;
import com.chkj.fbook.business.dao.SeatTimeDao;
import com.chkj.fbook.business.domain.SeatTimeDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SeatTimeServiceImpl implements SeatTimeService {

    @Resource
    private SeatTimeDao seatTimeDao;

    @Override
    public SeatTimeDO get(String id) {
        return seatTimeDao.get(id);
    }

    @Override
    public List<SeatTimeDO> list(Map<String, Object> map) {
        return seatTimeDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return seatTimeDao.count(map);
    }

    @Override
    @Transactional
    public int save(SeatTimeDO seatTime) {

        //已存在的排序自增
        seatTimeDao.editAdd1(seatTime);

        seatTime.setId(IDUtils.genUUID());
        Date date = new Date();
        seatTime.setCreateTime(date);
        seatTime.setUpdateTime(date);
        return seatTimeDao.save(seatTime);
    }

    @Override
    @Transactional
    public int update(SeatTimeDO seatTime) {

        //获取旧时间
        SeatTimeDO oldSeatTimeDO = this.get(seatTime.getId());

        //先排序自减
        seatTimeDao.editSub1(oldSeatTimeDO);

        //再排序自增
        seatTimeDao.editAdd1(seatTime);

        seatTime.setUpdateTime(new Date());
        return seatTimeDao.update(seatTime);
    }

    @Override
    @Transactional
    public int remove(String id) {

        //获取删除的房间
        SeatTimeDO oldSeatTimeDO = this.get(id);

        //已存在的排序自减
        seatTimeDao.editSub1(oldSeatTimeDO);

        return seatTimeDao.remove(id);
    }

    @Override
    @Transactional
    public int batchRemove(String[] ids) {
        //seatTimeDao.batchRemove(ids);
        for (String id : ids) {
            this.remove(id);
        }
        return 1;
    }

    @Override
    public List<SeatTimeDO> getAll() {
        return seatTimeDao.getAll();
    }

}
